/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.source.view;

import com.wisnu.spms.service.entity.DbBilling;
import com.wisnu.spms.service.entity.DbStudent;
import com.wisnu.spms.source.controller.PanelBillingController;
import com.wisnu.spms.source.listener.PanelBillingListener;
import com.wisnu.spms.source.model.PanelBillingModel;
import com.wisnu.spms.source.model.tableModel.PanelBillingTableModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Wisnu Wardoyo
 */
public class PanelBilling extends javax.swing.JPanel implements PanelBillingListener, ListSelectionListener {

    private PanelBillingController controller;
    private PanelBillingModel model;
    private PanelBillingTableModel tableModel;
    private boolean isReady = false;

    public PanelBilling(DbStudent student) {
        initComponents();
        model = new PanelBillingModel(this);
        controller = new PanelBillingController(model, student);
        tableModel = new PanelBillingTableModel();
        tableBilling.setModel(tableModel);
        tableBilling.getSelectionModel().addListSelectionListener(this);
        initComSemester(student);
        label(false);
    }

    public PanelBilling() {
    }

    public JComboBox getComSemester() {
        return comSemester;
    }

    public JTable getTableBilling() {
        return tableBilling;
    }

    public final void initComSemester(DbStudent student) {
        Integer semester = student.getStudentSemester();
        comSemester.removeAllItems();
        comSemester.addItem("Pilih Semester");
        for (int i = 0; i < semester; i++) {
            comSemester.addItem("Semester " + (i + 1));
        }
        isReady = true;
    }
    
    protected final void label(boolean isShow){
        labTitle.setVisible(isShow);
        labBillingAmount.setVisible(isShow);
        labBillingPeriod.setVisible(isShow);
        labBillingName.setVisible(isShow);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        comSemester = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableBilling = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        labTitle = new javax.swing.JLabel();
        labBillingName = new javax.swing.JLabel();
        labBillingAmount = new javax.swing.JLabel();
        labBillingPeriod = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Daftar Billing"));

        jLabel1.setText("Semester :");

        comSemester.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pilih Semester", "Semester 1" }));
        comSemester.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comSemesterActionPerformed(evt);
            }
        });

        tableBilling.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableBilling);

        jButton1.setText("Tambah Billing");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        labTitle.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        labTitle.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        labTitle.setText("Rincian Billing");

        labBillingName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        labBillingName.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        labBillingName.setText("jLabel3");

        labBillingAmount.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        labBillingAmount.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        labBillingAmount.setText("jLabel4");

        labBillingPeriod.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        labBillingPeriod.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        labBillingPeriod.setText("jLabel5");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(labBillingName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labBillingAmount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(comSemester, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton1)
                            .addComponent(labTitle))
                        .addGap(0, 108, Short.MAX_VALUE))
                    .addComponent(labBillingPeriod, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(comSemester, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(labTitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labBillingName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labBillingAmount)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labBillingPeriod)
                .addContainerGap(74, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void comSemesterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comSemesterActionPerformed
        if (isReady) {
            controller.changeSemester(this);
        }
    }//GEN-LAST:event_comSemesterActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        controller.addBillingType(this);
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comSemester;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labBillingAmount;
    private javax.swing.JLabel labBillingName;
    private javax.swing.JLabel labBillingPeriod;
    private javax.swing.JLabel labTitle;
    private javax.swing.JTable tableBilling;
    // End of variables declaration//GEN-END:variables

    @Override
    public void onChange(PanelBillingModel model) {
        tableModel = new PanelBillingTableModel();
        tableModel.setList(model.getList());
        tableBilling.setModel(tableModel);
        label(false);
    }

    @Override
    public void valueChanged(ListSelectionEvent lse) {
        label(true);
        try {
            DbBilling billing = tableModel.get(tableBilling.getSelectedRow());
            labBillingName.setText(billing.getDbBillingtype().getBillingTypeName());
            labBillingAmount.setText("Harga : Rp."+billing.getDbBillingtype().getBillingAmount()+"");
            labBillingPeriod.setText("Wajib Diangsur : "+billing.getDbBillingtype().getBillingPeriod()+" Kali");
        } catch (IndexOutOfBoundsException ex) {

        }
    }
}
